package com.fancy.learncenter.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.fragment.DailyBeautyFragment;
import com.fancy.learncenter.fragment.KwDiffAndEasyFragment;
import com.fancy.learncenter.fragment.MyselfFragment;
import com.fancy.learncenter.fragment.OnlineReviewsFragment;
import com.fancy.learncenter.fragment.TurandotFragment;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.teeya.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @Bind(R.id.framelayout_contain)
    FrameLayout framelayoutContain;
    @Bind(R.id.beauty)
    TextView beauty;
    @Bind(R.id.easy)
    TextView easy;
    @Bind(R.id.online)
    TextView online;
    @Bind(R.id.duld)
    TextView duld;
    @Bind(R.id.myself)
    TextView myself;
    @Bind(R.id.bottom)
    LinearLayout bottom;
    @Bind(R.id.img)
    SimpleDraweeView img;

    private DailyBeautyFragment beautyFragment;
    private KwDiffAndEasyFragment kwDiffAndEasyFragment;
    private OnlineReviewsFragment onlineReviewsFragment;
    private TurandotFragment turandotFragment;
    private MyselfFragment myselfFragment;
    private Fragment tempFrag;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("主界面");
        showIndex(1);

        img.setImageURI("http://58pic.oopic.com/58pic/16/03/00/99658PICkIX_1024.jpg");
        HttpMehtod.getInstance().testRetrofit();
    }

    private void showIndex(int index) {
        switch (index) {
            case 1:
                transaction = getSupportFragmentManager().beginTransaction();
                if (beautyFragment == null) {
                    beautyFragment = new DailyBeautyFragment();
                    transaction.add(R.id.framelayout_contain, beautyFragment);
                }

                if (tempFrag == beautyFragment) {
                    return;
                } else if (tempFrag != null) {
                    transaction.hide(tempFrag);
                } else {
                    tempFrag = beautyFragment;
                }
                transaction.show(beautyFragment);
                transaction.commit();
                tempFrag = beautyFragment;
                break;
            case 2:
                transaction = getSupportFragmentManager().beginTransaction();
                if (kwDiffAndEasyFragment == null) {
                    kwDiffAndEasyFragment = new KwDiffAndEasyFragment();
                    transaction.add(R.id.framelayout_contain, kwDiffAndEasyFragment);
                }

                if (tempFrag == kwDiffAndEasyFragment) {
                    return;
                } else if (tempFrag != null) {
                    transaction.hide(tempFrag);
                }
                transaction.show(kwDiffAndEasyFragment);
                transaction.commit();
                tempFrag = kwDiffAndEasyFragment;
                break;
            case 3:
                transaction = getSupportFragmentManager().beginTransaction();
                if (onlineReviewsFragment == null) {
                    onlineReviewsFragment = new OnlineReviewsFragment();
                    transaction.add(R.id.framelayout_contain, onlineReviewsFragment);
                }
                if (tempFrag == onlineReviewsFragment) {
                    return;
                } else if (tempFrag != null) {
                    transaction.hide(tempFrag);
                }
                transaction.show(onlineReviewsFragment);
                transaction.commit();

                tempFrag = onlineReviewsFragment;

                break;
            case 4:
                transaction = getSupportFragmentManager().beginTransaction();
                if (turandotFragment == null) {
                    turandotFragment = new TurandotFragment();
                    transaction.add(R.id.framelayout_contain, turandotFragment);
                }
                if (tempFrag == turandotFragment) {
                    return;
                } else if (tempFrag != null) {
                    transaction.hide(tempFrag);
                }
                transaction.show(turandotFragment);
                transaction.commit();
                tempFrag = turandotFragment;
                break;
            case 5:
                transaction = getSupportFragmentManager().beginTransaction();
                if (myselfFragment == null) {
                    myselfFragment = new MyselfFragment();
                    transaction.add(R.id.framelayout_contain, myselfFragment);
                }
                if (tempFrag == myselfFragment) {
                    return;
                } else if (tempFrag != null) {
                    transaction.hide(tempFrag);
                }
                transaction.show(myselfFragment);
                transaction.commit();
                tempFrag = myselfFragment;
                break;
        }
    }

    @OnClick({R.id.framelayout_contain, R.id.beauty, R.id.easy, R.id.online, R.id.duld, R.id.myself, R.id.bottom})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.framelayout_contain:
                break;
            case R.id.beauty:
                showIndex(1);
                break;
            case R.id.easy:
                showIndex(2);
                break;
            case R.id.online:
                showIndex(3);
                break;
            case R.id.duld:
                showIndex(4);
                break;
            case R.id.myself:
                showIndex(5);
                break;
            case R.id.bottom:
                break;
        }
    }
}
